package com.atguigu.marketing.mapper;

import com.atguigu.marketing.bean.MarketingRule;
import com.atguigu.marketing.bean.MarketingRuleAllInfo;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 营销规则 Mapper 接口
 * </p>
 *
 * @author zhangchen
 * @since 2023-02-02
 */

@Mapper
@DS("marketing")
public interface MarketingRuleMapper extends BaseMapper<MarketingRule> {


    @Select("select mr.id,mr.marketing_rule_name,mr.is_enable ,mr.create_time,mr.update_time,\n" +
            "    mt.id,mt.marketing_rule_id,\n" +
            "    mt.push_type,mt.loop_type,\n" +
            "    mt.loop_time,mt.once_time,mt.loop_month_day,mt.loop_week_day,from_dt,to_dt,\n" +
            "   mt.trigger_event,"+
            "    mg.id,mg.marketing_rule_id,mg.group_ids_json,  mg.user_num,\n" +
            "    mc.id,mc.channel_type \n" +
            " from marketing_rule mr\n" +
            "    left join marketing_rule_time mt on  mr.id=mt.marketing_rule_id\n" +
            "    left join  marketing_rule_group mg on mr.id=mg.marketing_rule_id\n" +
            "    left join  marketing_rule_channel mc on mr.id=mc.marketing_rule_id  " +
            "  order by mr.id desc")
    @Results({
            @Result(property ="id" ,column ="id"  ),
            @Result(property ="marketingRuleName" ,column ="marketing_rule_name"  ),
            @Result(property ="isEnable" ,column ="is_enable"  ),
            @Result(property ="createTime" ,column ="create_time"  ),
            @Result(property ="updateTime" ,column ="update_time"  ),
            @Result(property ="marketingRuleTime.pushType" ,column ="push_type"  ),
            @Result(property ="marketingRuleTime.loopType" ,column ="loop_type"  ),
            @Result(property ="marketingRuleTime.onceTime" ,column ="once_time"  ),
            @Result(property ="marketingRuleTime.loopTime" ,column ="loop_time"  ),
            @Result(property ="marketingRuleTime.loopMonthDay" ,column ="loop_month_day"  ),
            @Result(property ="marketingRuleTime.loopWeekDay" ,column ="loop_week_day"  ),
            @Result(property ="marketingRuleTime.triggerEvent" ,column ="trigger_event"  ),
            @Result(property ="marketingRuleTime.fromDt" ,column ="from_dt"  ),
            @Result(property ="marketingRuleTime.toDt" ,column ="to_dt"  ),
            @Result(property ="marketingRuleGroup.groupIds" ,column ="group_ids_json" ,typeHandler = JacksonTypeHandler.class),
            @Result(property ="marketingRuleGroup.userNum" ,column ="user_num"  ),
            @Result(property ="marketingRuleChannel.channelType" ,column ="channel_type"  ),

    })
    public List<MarketingRule> selectAllMarketingRuleList();

    @Select("select  mr.id,mt.push_type,mt.loop_time,mt.loop_type," +
            "mt.loop_month_day, mt.loop_week_day,mt.once_time, " +
            "  mg.group_ids_json ,mg.user_actions_today_json      ," +
            " mc.channel_type , mc.limit_time_num,mc.limit_time_unit, " +
            " mcm.model_title, mcm.model_text,mcm.url " +
            "  from marketing_rule mr " +
            "  inner join marketing_rule_channel mc on mr.id=mc.marketing_rule_id " +
            "  inner join marketing_rule_group mg on mr.id=mg.marketing_rule_id " +
            "  inner join marketing_rule_time mt on mr.id=mt.marketing_rule_id " +
            "  inner join  marketing_rule_channel_model  mcm ON mcm.id=mc.channel_model_id " +
            "  where mr.is_enable='1'  " +
            "  and (mt.from_dt is null or mt.from_dt < now() ) and (mt.to_dt is null or mt.to_dt >now()) " +
            "  and mt.push_type in ('1','2')  ")
    @Results( {
            @Result(property ="groupIds" ,column ="group_ids_json" ,typeHandler = FastjsonTypeHandler.class),
            @Result(property ="userActionList" ,column ="user_actions_today_json" ,typeHandler = FastjsonTypeHandler.class)
    } )
    public List<MarketingRuleAllInfo> selectMarketingRuleListForTask();
}
